package com.lebang.controller.api;

import com.lebang.aop.Sessionid;
import com.lebang.common.MemberLock;
import com.lebang.common.Require;
import com.lebang.domain.MemberAdStageRecord;
import com.lebang.domain.MemberAdvertisementRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.model.PointModel;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.LuckDrawService;
import com.lebang.service.PointService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @Author: hwq
 * @Date: 2019/11/26 16:16
 * @Version 1.0
 */
@Api(tags = "转盘抽奖活动")
@RequestMapping("/luckdraw")
@RestController
public class LuckDrawController {
    @Autowired
    private LuckDrawService luckDrawService;
    public static final String LUCKDRAW_BRAND_SET = "LUCKDRAW_BRAND_SET:";

    public static final String LUCKDRAW_HASH_MEMBER = "LUCKDRAW_HASH_MEMBER:";
    @Autowired
    private PointService pointService;

    @ApiOperation("获取今日剩余抽奖信息")
    @Sessionid
    @Require
    @PostMapping("/count")
    public JsonData count(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        return Callback.success("success", "查询成功", 100 - luckDrawService.todayInit(memberId)
        );
    }

    @ApiOperation("抽奖")
    @Sessionid
    @Require
    @PostMapping("/luck")
    public JsonData luck(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String brand = requestHeaders.getAppStore();
        String memberId = requestHeaders.getUserid();
        int version = Integer.parseInt(requestHeaders.getVersion());
        if (version<=0){
            return Callback.fail("200","版本错误！");
        }
        MemberAdvertisementRecord record;
        //获取今日抽奖次数
        int count = luckDrawService.todayInit(memberId);
        if (count >= 100) {
            return Callback.fail("200", "今日次数已用完明天再来", null);
        }

        if (getLuck()) {
            HashMap map = new HashMap();
            PointModel pointModel = luckDrawService.getPointSet();
            if (pointModel == null) {
                return Callback.fail("201", "服务器错误");
            }
            //获取几次抽奖以后不翻倍
            int i = luckDrawService.getPointAfterDouble();
            //获取几次抽奖以后会出现广告
            int n = luckDrawService.getPointAfterAd();
            if (count + 1 > n) {
                if (getLuck()) {
                    map.put("recordInfo", luckDrawService.selectVideoOrGift(brand,version));
                    map.put("hasRecordInfo", true);
                } else {
                    map.put("hasRecordInfo", false);
                }
            } else {
                map.put("hasRecordInfo", false);
            }
            //获取随机金币
            int point = RandomUtils.nextInt(pointModel.getStar(), pointModel.getEnd() + 1);
            //前n次必定金币翻倍
            if (count + 1 <= i) {
                record = luckDrawService.addPointRecord(memberId, point, 1);
                map.put("ad", true);
                map.put("adInfo", luckDrawService.selectAdByBrand(brand,version));
            } else {
                //后续50%金币翻倍
                if (getLuck()) {
                    record = luckDrawService.addPointRecord(memberId, point, 1);
                    map.put("ad", true);
                    map.put("adInfo", luckDrawService.selectAdByBrand(brand,version));
                } else {
                    map.put("ad", false);
                    record = luckDrawService.addPointRecord(memberId, point, 0);
                }
            }
            pointService.addPoint(memberId, point, TaskCategory.ZPCJ, requestHeaders,"");
            map.put("record", record);
            //抽奖次数+1
            luckDrawService.addCount(memberId);
            luckDrawService.checkCount(memberId, count + 1);
            return Callback.success("201", "金币", map);
        } else {
            //礼物
            //抽奖次数+1
            luckDrawService.addCount(memberId);
            luckDrawService.checkCount(memberId, count + 1);
            return Callback.success("202", "礼物", luckDrawService.selectGiftByBrand(brand,version));
        }

    }

    @ApiOperation("首页广告")
    @Require
    @Sessionid
    @RequestMapping("/home")
    public JsonData home(HttpServletRequest request){
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String brand = requestHeaders.getAppStore();
        int version = Integer.parseInt(requestHeaders.getVersion());
        if (version<=0){
            return Callback.fail("200","版本错误！");
        }
        return Callback.success("203","首页广告",luckDrawService.selectHomeByBrand(brand,version));
    }

    /**
     * 50几率返回true
     *
     * @return
     */
    public boolean getLuck() {
        int randomInt = RandomUtils.nextInt(1, 3);
        return randomInt > 1;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            int randomInt = RandomUtils.nextInt(1, 3);
       //     System.out.println(randomInt);
        }
    }

    @ApiOperation("领取翻倍奖励")
    @Sessionid
    @Require
    @PostMapping("/double")
    @MemberLock
    public JsonData db(HttpServletRequest request, Integer id) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        MemberAdvertisementRecord record = luckDrawService.selectRecordById(id);
        if (record == null) {
            return Callback.fail("empty", "非法参数", null);
        }
        if (!memberId.equals(record.getMemberId())) {
            return Callback.fail("empty", "非法参数", null);
        }
        if (record.getStatus().equals(0)) {
            return Callback.fail("empty", "非法参数", null);
        }
        if (record.getIsDouble().equals(1)) {
            return Callback.fail("empty", "已翻倍", null);
        }
        luckDrawService.updateRecord(record);
        pointService.addPoint(memberId, record.getMoney() * 2, TaskCategory.ZPCJ, new RequestHeaders(request),"");
        return Callback.success("success", "获取成功", record.getMoney() * 2);
    }

    @ApiOperation("查询阶段性红包")
    @Sessionid
    @Require
    @PostMapping("/stage/list")
    public JsonData stage(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        return Callback.success("success", "获取成功", luckDrawService.selectStageRecordList(memberId));
    }

    @ApiOperation("领取阶段性红包")
    @Sessionid
    @Require
    @PostMapping("/stage/get")
    @MemberLock
    public JsonData get(HttpServletRequest request, Integer id) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        MemberAdStageRecord record = luckDrawService.selectStageById(id);
        if (record == null) {
            return Callback.fail("empty", "非法参数", null);
        }
        if (!memberId.equals(record.getMemberId())) {
            return Callback.fail("empty", "非法参数", null);
        }
        if (record.getStatus().equals(1)) {
            return Callback.fail("empty", "已领取", null);
        }
        luckDrawService.updateStageRecord(record);
        pointService.addPoint(memberId, record.getMoney(), TaskCategory.ZPCJ, new RequestHeaders(request),"");
        return Callback.success("success", "获取成功", luckDrawService.selectStageRecordList(memberId));
    }

}

